<template>
  <div>
    <div @click="showPopup">{{ cityname }}<van-icon name="arrow-down" /></div>
    <van-popup class="popup" v-model="show" closeable position="top">
      <div>
        <h2 class="title">选择城市</h2>
        <van-index-bar>
          <div v-for="(value, key) in city" :key="key">
            <van-index-anchor :index="key" class="item"/>
            <van-cell v-for="item in value" :key="item.regionName" :title="item.regionName"
              @click="(item.regionName === '北京') || (item.regionName === '上海') || (item.regionName === '广州') ? change(item.regionName) : ''" />
          </div>
        </van-index-bar>
      </div>
    </van-popup>
  </div>
</template>

<script>
import { mapState, mapMutations } from 'vuex'

export default {
  name: 'City',
  props: ['city'],
  data() {
    return {
      show: false,
    }
  },
  computed: {
    ...mapState(['cityname'])
  },
  methods: {
    ...mapMutations(['changeCity']),

    showPopup() {
      this.show = true
    },

    // 改变城市
    change(name) {
      this.changeCity(name)
      this.show = false
      this.$emit('change')
    }
  }
}
</script>

<style lang='less' scoped>
.item {
  background-color: rgb(247, 248, 250);
}
.title {
  text-align: center;
  font-size: 20px;
  line-height: 55px;
  font-weight: 700;
}

.popup {
  height: 100%;
  text-align: left;
}
</style>